package com.az.alibaba.common.db;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public final class Db {

	private static final Logger log = LoggerFactory.getLogger(Db.class);

	private static DataSource ds;

	/**
	 * 获取数据源,如果失败返回null
	 * 
	 * @return
	 */
	public static DataSource getDs() {
		if (ds == null) {
			synchronized (Db.class) {
				if (ds == null) {
					ComboPooledDataSource cpds = new ComboPooledDataSource();
					try {
						cpds.setDriverClass(System.getProperty("driverClass"));
						cpds.setJdbcUrl(System.getProperty("jdbcUrl"));
						cpds.setUser(System.getProperty("user"));
						cpds.setPassword(System.getProperty("password"));
						ds = cpds;
					} catch (PropertyVetoException e) {
						log.error("初始化c3p0出错:", e);
					}
				}
			}
		}
		return ds;
	}
}
